import WeixinJSSDK from '@/common/utils/weixinJSSDK.js'
import { encrypt, decrypt } from "./crypto.js";
/* 工具 */
const defaultTools = [
  {
    id: "laserPPT",
    type: "laserPPT",
    text: "激光笔",
    icon: require('./images/laserPPT.png'),
    icon_active: require('./images/laserPPT-act.png'),
    // 默认事件处理函数
    onTap: (tool) => {
      // uni.showToast({ title: "激光-点击", icon: "none" });
    },
    onLongPressStart: (tool) => {
      actType = "laserPPT";
      // uni.showToast({ title: "激光-长按开始", icon: "none" });
      toggleTracking();
    },
    onRelease: (tool) => {
      // uni.showToast({ title: "激光-松开", icon: "none" });
      if (isTracking) {
        toggleTracking();
      }
    },
  },
  {
    id: "spotLight",
    type: "spotLight",
    text: "聚光灯",
    icon: require('./images/spotLight.png'),
    icon_active: require('./images/spotLight-act.png'),
    onTap: (tool) => {
      // uni.showToast({ title: "聚光-点击", icon: "none" });
    },
    onLongPressStart: (tool) => {
      // uni.showToast({ title: "聚光-长按开始", icon: "none" });
      actType = "spotLight";
      toggleTracking();
    },
    onRelease: (tool) => {
      // uni.showToast({ title: "聚光-松开", icon: "none" });
      if (isTracking) {
        toggleTracking();
      }
    },
  },
  {
    id: "activity",
    type: "activity",
    text: "添加活动",
    icon: require('./images/activity.png'),
    icon_active: require('./images/activity-act.png'),
    onTap: (tool) => {
      // uni.showToast({ title: "活动-点击", icon: "none" });
    },
    onLongPressStart: (tool) => {
      // uni.showToast({ title: "活动-长按开始", icon: "none" });
    },
    onRelease: (tool) => {
      // uni.showToast({ title: "活动-松开", icon: "none" });
    },
  },
  {
    id: "close",
    type: "close",
    text: "关闭投屏",
    icon: require('./images/close.png'),
    icon_active: require('./images/close-act.png'),
    onTap: (tool) => {
      // uni.showToast({ title: "关闭投屏-点击", icon: "none" });
      closeProjector();
    },
    onLongPressStart: (tool) => {
      // uni.showToast({ title: "关闭投屏-长按开始", icon: "none" });  
    },
    onRelease: (tool) => {
      // uni.showToast({ title: "关闭投屏-松开", icon: "none" });
    },
  },
  {
    id: "prevPage",
    type: "prevPage",
    icon: require('./images/prevPage.png'),
    icon_active: require('./images/prevPage-act.png'),
    text: "上一页",
    onTap: (tool) => {
      uni.showToast({ title: "上一页-点击", icon: "none" });
      sendFocusPointMessage({event: 'fileWrapEvent',type: 'prevPage'})
    },
  },
  {
    id: "nextPage",
    type: "nextPage",
    text: "下一页",
    icon: require('./images/nextPage.png'),
    icon_active: require('./images/nextPage-act.png'),
    onTap: (tool) => {
      uni.showToast({ title: "下一页-点击", icon: "none" });
      sendFocusPointMessage({event: 'fileWrapEvent',type: 'nextPage'})
    },
  },
];

/* socket连接 */
import { socket } from "./socket";
let isConnected = false;
const appId = uni.getSystemInfoSync().deviceId;  // 获取app唯一标识
let user_info = uni.getStorageSync('USER_INFO') || {}; // 获取用户信息

// 绑定数据
let bindData = {  //绑定的pc的用户数据
  user_id: user_info.aid,
  pc_code: "7f3d23c991121e2dca6ad44bb2e0f085",
};
// 监听连接事件
socket.on("connect", () => {
  isConnected = true;
  console.log("已连接到服务器1");
});
// 监听断开连接事件
socket.on("disconnect", () => {
  isConnected = false;
  console.log("已断开连接1");
});
// 发送通知到pc端
const sendFocusPointMessage = (position = {}) => {
  let eventType = position.event || "focusPointEvent";
  let x = position.x || 0;
  let y = position.y || 0;
  let z = position.z || 0;
  let cmd = position.cmd || "";
  let type = position.type || "";
  socket.emit("customEvent", {
    event: eventType,
    needSelf: true,
    data: {
      from: user_info.aid,
      to: bindData.user_id,
      to_code: bindData.pc_code,
      from_code: appId,
      x,
      y,
      z,
      cmd,
      type,
    },
  });
};

/* 陀螺仪-激光笔、聚光 */
import { GyroscopePlugin } from './gyroscope.js';
let isTracking = false; // 是否正在追踪
let actType = ''; // 当前活动类型
// 创建陀螺仪实例
const gyroscope = new GyroscopePlugin({
  threshold: 0.1,
  onDataChange: (data) => {
    // 更新移动数据
    console.log('发送数据到服务器', data);
    // 发送数据到服务器
    sendMoveData(data);
  },
  onError: (message) => {
    uni.showToast({
      title: message,
      icon: "none",
    });
  }
});
// 切换追踪状态
const toggleTracking = () => {
  // 通知pc数据修改状态
  sendFocusPointMessage({
    cmd: actType,
    type: isTracking ? "end" : "start",
  });
  
  // 开始/停止追踪
  isTracking = gyroscope.toggle();
  
  if (isTracking) {
    uni.showToast({
      title: "开始监听陀螺仪",
      icon: "success",
    });
  }
};
// 发送移动数据
const sendMoveData = (moveData) => {
  // 这里使用WebSocket示例
  console.log("通知客户端", moveData);
  sendFocusPointMessage({
    x: moveData.x,
    y: moveData.y,
    z: moveData.z,
    cmd: actType,
    type: "move",
  });
};

// h5扫码绑定
let isInitWx = false; // 是否初始化微信
const scanQRCode = async () => {
  console.log('扫码绑定');
  if (!isInitWx) {
    let initRes = await WeixinJSSDK.init();
    isInitWx = !!initRes;
  }
  WeixinJSSDK.scanQRCode({
    needResult: 1,
    scanType: ["qrCode"],
    success: (res) => {
      console.log('扫码成功', res);
      parseQRCode(res.resultStr);
    },
    fail: (err) => {
      console.log('扫码失败', err);
    }
  });
}

// 解析二维码内容，绑定pc
const parseQRCode = (qrCode) => {
  let data = JSON.parse(decrypt(qrCode));
  console.log('解析二维码内容', data);
  bindData = data;
}

// 关闭投屏
const closeProjector = () => {
  bindData = {}
}


export {
  bindData,
  defaultTools,
  scanQRCode,
}